package com.example.goodpet.activity.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.goodpet.R;
import com.example.goodpet.activity.UserInfoActivity;
import com.example.goodpet.activity.adapter.UserInfoPetAdapter;
import com.example.goodpet.base.BaseFragmentForView;
import com.example.goodpet.bean.PetInformation;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.common.EndlessRecyclerOnScrollListener;
import com.example.goodpet.common.LoadMoreWrapper;
import com.example.goodpet.common.LoadingPage;
import com.example.goodpet.service.UserService;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class UserInfoPetFragment extends BaseFragmentForView {
    private RecyclerView recyclerView;
    public List<UserPetInfo> list=new ArrayList<>();
    private UserPetInfo userPetInfo;

    private Retrofit retrofit;
    private UserService userService;



    @Override
    protected void LoadPage() {
        loadingPage.setStateCurrent(LoadingPage.STATE_loading);
        userService.getMyRelease(userPetInfo.getUserId(),1).enqueue(new Callback<ResponseBody<List<PetInformation>>>() {
            @Override
            public void onResponse(Call<ResponseBody<List<PetInformation>>> call, Response<ResponseBody<List<PetInformation>>> response) {
                ResponseBody<List<PetInformation>> body = response.body();
                if (body!=null){
                    if (body.getCode()==200){
                        loadingPage.setStateCurrent(LoadingPage.STATE_SUCCESS);
                        List<PetInformation> data = body.getData();
                        for(int i=0; i<data.size(); i++){
                            PetInformation petInformation = data.get(i);
                            list.add(new UserPetInfo(userPetInfo.getUserId(),userPetInfo.getPhoneNumber(),userPetInfo.getNickname(),userPetInfo.getPortrait(),userPetInfo.getGender(),userPetInfo.getSignature(),petInformation.getId(),petInformation.getCategory(),petInformation.getOverview(),petInformation.getPlace(),String.valueOf(petInformation.getPostdate()),petInformation.getType(),petInformation.getPetImage()));
                        }
                        UserInfoPetAdapter userInfoPetAdapter = new UserInfoPetAdapter(list,getActivity());
                        LoadMoreWrapper loadMoreWrapper = new LoadMoreWrapper(userInfoPetAdapter);
                        recyclerView.setAdapter(loadMoreWrapper);
                        recyclerView.setLayoutManager(new GridLayoutManager(UIUtils.getContext(),1));
                        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
                            @Override
                            public void onLoadMore() {
                                userService.getMyRelease(userPetInfo.getUserId(), ++loadMoreWrapper.page).enqueue(new Callback<ResponseBody<List<PetInformation>>>() {
                                    @Override
                                    public void onResponse(Call<ResponseBody<List<PetInformation>>> call, Response<ResponseBody<List<PetInformation>>> response) {
                                        loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING);
                                        ResponseBody<List<PetInformation>> body = response.body();
                                        if (body != null) {
                                            if (body.getCode() == 200) {
                                                List<PetInformation> data = body.getData();
                                                for (int i = 0; i < data.size(); i++) {
                                                    PetInformation petInformation = data.get(i);
                                                    list.add(new UserPetInfo(userPetInfo.getUserId(), userPetInfo.getPhoneNumber(), userPetInfo.getNickname(), userPetInfo.getPortrait(), userPetInfo.getGender(), userPetInfo.getSignature(), petInformation.getId(), petInformation.getCategory(), petInformation.getOverview(), petInformation.getPlace(), String.valueOf(petInformation.getPostdate()), petInformation.getType(), petInformation.getPetImage()));
                                                }
                                                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_COMPLETE);
                                            }else if (body.getCode()==404){
                                                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_END);
                                            }
                                        }
                                    }

                                    @Override
                                    public void onFailure(Call<ResponseBody<List<PetInformation>>> call, Throwable t) {
                                        t.printStackTrace();
                                        UIUtils.toast("连接服务器失败",false);
                                    }
                                });
                            }
                        });
                    }else if (body.getCode()==404){
                        loadingPage.setStateCurrent(LoadingPage.STATE_EMPTY);
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody<List<PetInformation>>> call, Throwable t) {
                loadingPage.setStateCurrent(LoadingPage.STATE_ERROR);
            }
        });
    }

    @Override
    public void initListener() {
        super.initListener();
    }

    protected void initData() {
        userPetInfo=((UserInfoActivity)getActivity()).userPetInfo;
        retrofit= RetrofitSingleton.getRetrofitNetConfig();
        userService = retrofit.create(UserService.class);
    }

    @Override
    protected void initView(View inflate) {
        super.initView(inflate);
        recyclerView=inflate.findViewById(R.id.recyclerView);
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_user_info_rv;
    }
}
